package com.thor.palmmall.dao.banlancebill;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Query;

import org.apache.log4j.helpers.OptionConverter;

import com.thor.commons.util.ConverterUtil;
import com.thor.commons.util.StringUtil;
import com.thor.dao.BasicDao;
import com.thor.palmmall.rest.balancebill.BanlanceBill;
import com.thor.palmmall.rest.balancebill.BanlanceBillDetail;
import com.thor.palmmall.rest.balancebill.BillQueryParam;
import com.thor.trans.ThorTX;

public class BanlanceBillDaoImpl  extends BasicDao implements BanlanceBillDao {

  @Override
  @ThorTX
  public String save(BanlanceBill banlanceBill) {
    PBanlanceBill perz = new PBanlanceBillConverter().convert(banlanceBill);
    perz.setLastModified(new Date());
    if (perz.getUuid() == null){
        em.persist(perz);
    }else{
        em.merge(perz);
    }
    em.flush();
    return perz.getUuid();
  }

  @Override
  @ThorTX
  public void deleteOldBill(String billSettle, String contracts, String storeId) {
    String hql = "delete from " + PBanlanceBill.class.getName()
        + " o where o.contract.uuid = :contracts and o.billSettle = :billSettle " ;
    if(storeId != null ){
      hql += " and o.store.uuid = :storeId" ;
    }
    Query query = em.createQuery(hql);
    query.setParameter("contracts", contracts);
    query.setParameter("billSettle", billSettle);
    if(storeId != null ){
      query.setParameter("storeId", storeId);
    }
    query.executeUpdate();
  }

  @Override
  public List<BanlanceBill> getBanlanceBills(BillQueryParam billQueryParam,List<String> billSettles) {
    List<BanlanceBill> billList = new ArrayList<BanlanceBill>();
    String hql = " from  " + PBanlanceBill.class.getName() 
        + " o where 1=1  " ;
    if(!StringUtil.isNullOrBlank(billQueryParam.getBillNo())){
      hql += " and o.billNo = :billNo ";
    }
    if(!StringUtil.isNullOrBlank(billQueryParam.getKeyword())){
      hql += " and (o.tenant.name like :keyword1  or o.contract like :keyword2 ) ";
    }
    Query query = em.createQuery(hql);
    if(!StringUtil.isNullOrBlank(billQueryParam.getBillNo())){
      query.setParameter("billNo", billQueryParam.getBillNo());
    }
    if(!StringUtil.isNullOrBlank(billQueryParam.getKeyword())){
      query.setParameter("keyword1", billQueryParam.getKeyword());
      query.setParameter("keyword2", billQueryParam.getKeyword());
    }
    List<PBanlanceBill> list = query.getResultList();
    if (list.isEmpty()){
        return null;
    }
    for (PBanlanceBill pBanlanceBill : list) {
       BanlanceBill banlanceBill  = new BanlanceBillConverter().convert(pBanlanceBill);
       billList.add(banlanceBill);
    }
    return billList;
  }

  @Override
  public List<BanlanceBillDetail> getBanlanceBills(String billUuid) {
    String hql = " from  " + PBanlanceBillDetail.class.getName() + " o where billUuid = :billUuid" ;
    Query query = em.createQuery(hql);
    query.setParameter("billUuid", billUuid);
    List<PBanlanceBillDetail> list = query.getResultList();
    if (list.isEmpty()){
      return null;
    }
    return ConverterUtil.convert(list, new BanlanceBillDetailConvert());
  }

  @Override
  public String getBanlancebillByCode(String code, String billSettle) {
    String hql = " from  " + PBanlanceBill.class.getName() 
        + " o where o.contract.code = :code and o.billSettle = :billSettle   " ;
    Query query = em.createQuery(hql);
    query.setParameter("code", code);
    query.setParameter("billSettle", billSettle);
    List<PBanlanceBill> list = query.getResultList();
    if (list.isEmpty()){
        return null;
    }else{
      return list.get(0).getUuid();
    }
  }

  @Override
  public BanlanceBillDetail getDetailByAccId(String accId) {
    String hql = " from  " + PBanlanceBillDetail.class.getName() 
        + " o where o.accId = :accId " ;
    Query query = em.createQuery(hql);
    query.setParameter("accId", accId);
    List<PBanlanceBillDetail> list = query.getResultList();
    if (list.isEmpty()){
        return null;
    }else{
      return new BanlanceBillDetailConvert().convert(list.get(0));
    }
  }

  @Override
  public BanlanceBill getBillByAccId(String accId) {
    String hql = " select o from  " + PBanlanceBill.class.getName() + " o," + PBanlanceBillDetail.class.getName() 
        + " p where o.uuid = p.bill.uuid and p.accId = :accId " ;
    Query query = em.createQuery(hql);
    query.setParameter("accId", accId);
    List<PBanlanceBill> list = query.getResultList();
    if (list.isEmpty()){
        return null;
    }else{
      return BanlanceBillConverter.getInstance().convert(list.get(0));
    }
  }

  @Override
  public BanlanceBill get(String billId) {
    String hql = " from  " + PBanlanceBill.class.getName() 
        + " o where o.uuid =  :billId " ;
    Query query = em.createQuery(hql);
    query.setParameter("billId", billId);
    List<PBanlanceBill> list = query.getResultList();
    if (list.isEmpty()){
        return null;
    }else{
      return BanlanceBillConverter.getInstance().convert(list.get(0));
    }
  }

  @Override
  @ThorTX
  public void deleteOldDetail(List<String> actIds) {
    String hql = "delete from " + PBanlanceBillDetail.class.getName()
        + " o where o.accId in (:actIds)" ;
    Query query = em.createQuery(hql);
    query.setParameter("actIds", actIds);
    query.executeUpdate();
  }

  
  
}
